<template>
  <div>
    <a href="javascript:;" @click='closePage'></a>
  </div>
</template>

<script>
  export default {
		data() {
			return {
				dataurl:""
			};
		},
    methods: {
      getUrlRequest() {
        var url = location.search; //获取url中"?"符后的字串
				this.dataurl=url.split("=")[2]
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
          var str = url.substr(1);
          if (str.indexOf("&") != -1) {
            var strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
              theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
            }
          } else {
            theRequest[str.split("=")[0]] = unescape(str.split("=")[1]);
          }
        }
        return theRequest;
      },
      getOpenId(code){
        this.$http.get(this.$api.getopenid(code)).then(res => {
          if(res.data.code == 0) {
            sessionStorage.setItem('scope',true)
            sessionStorage.setItem('openid',res.data.userinfo.openid)
            sessionStorage.setItem('nickname',res.data.userinfo.nickname)
            sessionStorage.setItem('headimgurl',res.data.userinfo.headimgurl)
            let url=this.$api.urls()+this.dataurl+"/"+this.$route.params.id
            location.replace(url) // 刷新页面去除授权后带的code
          }else {
            this.closePage()
          }
        })
      },
      closePage () {
//        alert('关闭页面')
        // 重置window.opener用来获取打开当前窗口的窗口引用
        // 这里置为null,避免IE下弹出关闭页面确认框
        window.opener = null;
        // JS重写当前页面
        window.open("", "_self", "");
        // 顺理成章的关闭当前被重写的窗口
        window.close();
      },
      togetScope () {
        var myObj=this.getUrlRequest();
        if(myObj&&myObj["code"]) {/**/
          var code = myObj["code"];
          this.getOpenId(code)
        }else {/**/
          alert('授权失败')
        }
      },
    },
    created () {
      this.togetScope()
    }
  }
</script>

<style lang="less" rel=stylesheet/less>

</style>
